1
|
|
|
|
2
|
|
|
|
3
|
|
|
var arrayFuncs = { |
4
|
|
|
joinLines: function(windows = true){ |
5
|
|
|
var lines = this; |
6
|
|
|
return windows ? lines.join("\r\n") : lines.join("\n"); |
7
|
|
|
}, |
8
|
|
|
none:null |
9
|
|
|
}; |
10
|
|
|
|
11
|
|
|
// register funcs |
12
|
|
|
UB.registerFuncs(Array.prototype, arrayFuncs); |
|
|
|
|
13
|
|
|
|
14
|
|
|
|
15
|
|
|
|
16
|
|
|
|
17
|
|
|
var stringFuncs = { |
18
|
|
|
|
19
|
|
|
splitLines: function(trimAll = false, delBlanks = false){ |
20
|
|
|
var text = this; |
21
|
|
|
var lines = text.split("\r\n").join("\n").split("\r").join("\n").split("\n"); |
22
|
|
|
|
23
|
|
|
// trim all lines if wanted |
24
|
|
|
if (trimAll || delBlanks) { |
25
|
|
|
for (var l = 0, ll = lines.length;l<ll;l++){ |
26
|
|
|
|
27
|
|
|
// trim |
28
|
|
|
var text = String(lines[l]).replace(Regex.Trim, ""); |
|
|
|
|
29
|
|
|
|
30
|
|
|
// skip this line if blank |
31
|
|
|
if (delBlanks && text == "") { |
32
|
|
|
lines.splice(l, 1); |
33
|
|
|
l--; |
|
|
|
|
34
|
|
|
ll--; |
35
|
|
|
continue; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
// save |
39
|
|
|
lines[l] = text; |
40
|
|
|
} |
41
|
|
|
lines.length = ll; |
42
|
|
|
} |
43
|
|
|
return lines; |
44
|
|
|
}, |
45
|
|
|
|
46
|
|
|
|
47
|
|
|
normalizeNewlines: function(finalNewline = "\n"){ |
48
|
|
|
var text = this; |
49
|
|
|
return text.split("\r\n").join("\n").split("\r").join(finalNewline); |
50
|
|
|
}, |
51
|
|
|
lineCount: function(windowsNewline = false){ |
52
|
|
|
var text = this; |
53
|
|
|
return S.CountOf(text, windowsNewline ? "\r\n" : "\n") + 1; |
|
|
|
|
54
|
|
|
}, |
55
|
|
|
|
56
|
|
|
trimAndDelBlanks: function(windowsNewline = true){ |
57
|
|
|
var s = this; |
58
|
|
|
return s.splitLines(true, true).joinLines(windowsNewline); |
59
|
|
|
}, |
60
|
|
|
|
61
|
|
|
/** convert to single line, taking care to add spaces between the last word & first word of next line */ |
62
|
|
|
toSingleLine: function(sep = " "){ |
63
|
|
|
var s = this; |
64
|
|
|
s = s.trim(); |
65
|
|
|
if (S.IsMultiline(s, false)) { |
|
|
|
|
66
|
|
|
return s.splitLines(true, true).join(sep); |
67
|
|
|
} |
68
|
|
|
return s; |
69
|
|
|
}, |
70
|
|
|
|
71
|
|
|
/** given a string and a char index within it, calculates which line that char came on (0 based) */ |
72
|
|
|
lineIndexOfChar: function(charIndex){ |
73
|
|
|
var text = this; |
74
|
|
|
var lineNum = 0; |
75
|
|
|
for (var c = 0;c<charIndex;c++){ |
76
|
|
|
var code = text.charCodeAt(c); |
77
|
|
|
if (code == charCodes.NewlineN) { |
|
|
|
|
78
|
|
|
lineNum++; |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
return lineNum; |
82
|
|
|
}, |
83
|
|
|
|
84
|
|
|
/** given a string and a char index within it, calculates which character on the line that char came on (0 based) */ |
85
|
|
|
lineCharOfChar: function(charIndex){ |
86
|
|
|
var text = this; |
87
|
|
|
var lineStart = 0; |
88
|
|
|
for (var c = 0;c<charIndex;c++){ |
89
|
|
|
var code = text.charCodeAt(c); |
90
|
|
|
if (code == charCodes.NewlineN) { |
|
|
|
|
91
|
|
|
lineStart = c; |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
return charIndex - lineStart; |
95
|
|
|
}, |
96
|
|
|
|
97
|
|
|
none:null |
98
|
|
|
}; |
99
|
|
|
|
100
|
|
|
// register funcs |
101
|
|
|
UB.registerFuncs(String.prototype, stringFuncs); |
102
|
|
|
|
103
|
|
|
// top level |
104
|
|
|
UB.newlines = function(newlines = 1, windows = false){ |
105
|
|
|
var text = this; |
106
|
|
|
|
107
|
|
|
// quick |
108
|
|
|
if (newlines <= 0) { |
109
|
|
|
return text; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
// quick |
113
|
|
|
var char = windows?"\r\n":"\n"; |
114
|
|
|
if (newlines == 1) { |
|
|
|
|
115
|
|
|
return text + char; |
116
|
|
|
} |
117
|
|
|
if (newlines == 2) { |
118
|
|
|
return text + char + char; |
119
|
|
|
} |
120
|
|
|
if (newlines == 3) { |
121
|
|
|
return text + char + char + char; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
// slow |
125
|
|
|
return text + char.repeat(newlines).join(""); |
126
|
|
|
}; |
127
|
|
|
|
This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.
To learn more about declaring variables in Javascript, see the MDN.